package com.lwisniewski.pg.pulsehematometry.serial.main;

import gnu.io.CommPortIdentifier;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import gnu.io.UnsupportedCommOperationException;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.TooManyListenersException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.pulsehematometry.serial.setup.SerialPortReaderSetup;

public class SerialMain implements Runnable {
	
	private final static Logger LOGGER = LoggerFactory.getLogger(SerialMain.class);
	
	private static CommPortIdentifier portId;
	
	private static SerialPortReaderSetup setup = SerialPortReaderSetup.getInstance();

	private Thread readThread;

	public static void main(String[] args) {
		List<CommPortIdentifier> availablePortList = setup.getAvailableSerialPortList();
		
		LOGGER.info("Available serial port IDs: {}", availablePortList);
		
		//TODO: Listen for user input what port to choose
		for(CommPortIdentifier id : availablePortList) {
			if(id.getName() == "COM3") {
				portId = id;
			}
		}
		
		setup.openPort(portId);
		
		new SerialMain();
	}

	public SerialMain() {
		
		readThread = new Thread(this);
		readThread.start();
	}

	public void run() {
		// TODO Auto-generated method stub
		
	}

}
